// #include "ringQueue单生产消费者.hpp"
#include "ringQueue多生产消费者.hpp"
#include <pthread.h>
#include <time.h>
#include <unistd.h>
using namespace std;

void *consumer(void *args)
{
    ringQueue<int> *rq = static_cast<ringQueue<int> *>(args);
    // 消费->打印环形队列中的int
    while (1)
    {
        int data = 0;
        rq->pop(data);
        cout << "consumer consume data:" << data << endl;
        sleep(1);
    }
}
void *productor(void *args)
{
    ringQueue<int> *rq = static_cast<ringQueue<int> *>(args);
    // 生产->随机数(1-100)
    while (1)
    {
        int data = rand() % 100 + 1;
        rq->push(data);
        cout << "productor data have sent" << data << endl;
    }
}

int main()
{
    srand(time(0));
    ringQueue<int> *rq = new ringQueue<int>;
    // 两个线程代表生产者消费者进行生产活动和消费活动
    pthread_t p[3];
    pthread_t c[2];

    for(int i =0 ;i<3;i++)
        pthread_create(p+i, nullptr, consumer, (void *)rq);
    for(int i =0 ;i<2;i++)
        pthread_create(c+i, nullptr, productor, (void *)rq);
    for(int i =0 ;i<3;i++)
        pthread_join(p[i], nullptr);
    for(int i =0 ;i<2;i++)
        pthread_join(c[i], nullptr);

    return 0;
}

